<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <!--测试用，上线复原-->
    <!--<meta name="format-detection" content="telephone=no" />-->
    <!--<meta name="apple-mobile-web-app-capable" content="yes" />-->
    <!--<meta name="apple-touch-fullscreen" content="yes">-->
    <!--<meta content="telephone=no,email=no" name="format-detection" />-->
    <meta name="flexible" content="initial-dpr=2,maximum-dpr=3" />
    <!--去缓存-->
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0"><!--end-->
    <title>羊仓健康生活家</title>
    <link rel="stylesheet" href="css/style.css" />
    <link rel="stylesheet" href="css/common.css"/>
    <script type="text/javascript" src="js/jquery.min.js" ></script>
    <script type="text/javascript" src="js/basePathUtil.js" ></script>
    <!--弹出-->
    <script type="text/javascript" src="js/layer-mobile/layer.js" ></script>
    <script type="text/javascript">
        var basePath=basePath();
        var access_token = GetUrl("access_token");
        $(document).ready(function(){
        });
    </script>

</head>
<body class="bg_f2">
<div class="addressList" id="addressList">
    <img src="img/address_top.png" alt="" class="address_top"/>
    <!--<div class="item">-->
        <!--<div class="contact">-->
            <!--<div class="name">enidsal</div>-->
            <!--<div class="phone">130****0000</div>-->
        <!--</div>-->
        <!--<div class="adress_detail">-->
            <!--<div class="select">-->
                <!--<img src="img/sigin.png" alt=""/>-->
            <!--</div>-->
            <!--<div>北京市朝阳区印花冻结</div>-->
        <!--</div>-->
        <!--<div class="img_btn">-->
            <!--<a><img src="img/edit.png" alt=""/></a>-->
            <!--<a><img src="img/del_gray.png" alt=""/></a>-->
        <!--</div>-->
    <!--</div>-->
</div>
<div class="btn_group">
    <a href="javascript:void(0);" onclick="addAddress()">新建</a>
</div>
</body>
<script>
    $(function(){
    });
    /**
     * 删除地址
     * @param id
     */
    function deleteAddress(id) {
        $.ajax({
            type:"POST",
            url:basePath+"/v1/user/address/delete?access_token="+access_token,
            dataType : 'json',
            data :{
                id:id
            },
            success:function(data){
                if(data.code==1000) {
                    layer.open({
                        content: '删除成功',
                        skin: 'msg',
                        time: 1,
                        end: function (index) {
                            window.location.reload();
                        }
                    });
                }else{
                    layer.open({content: data.msg,skin: 'msg',time: 2});
                }
            },
            error:function(data) {
            }
        });
    }
    function addAddress() {
        var toUrl = basePath+"res/addressAdd.html?access_token="+access_token+"&t="+new Date().getTime();
        //判断是否从下单页面进入的
        if(GetUrl("ids").length!=0 || GetUrl("id").length!=0){
           if(GetUrl("id").length>0){
                toUrl+='&id='+GetUrl("id");
            }
            if(GetUrl("ids").length>0){
                toUrl+='&ids='+GetUrl("ids");
            }
        }
        window.location.href=toUrl;
    }
    /**
     * 删除地址
     * @param id
     */
    function deleteAddressUI(id){
        layer.open({
            content: '您确定要删除地址？'
            ,btn: ['我点错了', '确认删除']
            ,skin: 'footer'
            ,yes: function(index){
                layer.close(index);
            },
            no:function (index) {
                deleteAddress(id);
                layer.close(index);
            }
        });
        return ;
    }
    /**
     * 编辑地址
     * @param id
     */
    function editAddress(id){
        window.location.href=basePath+"res/addressEdit.html?access_token="+access_token+'&id='+id+"&t="+new Date().getTime();
    }
    /**
     * 地址信息
     * @param addressDetails
     */
    function item(addressDetails) {
        var item = '';
        item+='<div class="item" id="'+addressDetails.id+'">';
        item+='<div class="contact">';
        item+='<div class="name">'+addressDetails.contactName+'</div>';
        item+='<div class="phone">'+addressDetails.contactMobile+'</div>';
        item+='</div>';
        item+='<div class="adress_detail">';
        item+='<div class="select"';
        if(addressDetails.isDefault !=1){ item+='style="visibility: hidden;"' }
        item+='><img src="img/sigin.png" alt=""/></div>';
        item+='<div>'+addressDetails.address+''+addressDetails.contactAddress+'</div>';
        item+='</div>';
        item+='<div class="img_btn">';
        item+='<a onclick="editAddress('+addressDetails.id+')"><img src="img/edit.png" alt=""/></a>';
        item+='<a onclick="deleteAddressUI('+addressDetails.id+')"><img src="img/del_gray.png" alt=""/></a>';
        item+='</div>';
        item+='</div>';
        $("#addressList").append(item);
    }
    /**
     * 地址列表
     */
    function addressList() {
        $.ajax({
            type:"GET",
            url:basePath+"/v1/user/address/showList?access_token="+access_token,
            dataType : 'json',
            success:function(data){
                if(data.code==1000) {
                    //有数据
                    if(data.data.length>0){
                        $.each(data.data, function(index, content){
                            item(data.data[index]);
                        });
                    }else{
                        $("#addressList").hide();
                        $(".bg_f2").append('<div class="address"><img src="img/address.png" alt=""></div>');
                    }
                    //判断是否从下单页面进入的
                    if(GetUrl("ids").length!=0 || GetUrl("id").length!=0){
                        //修改样式
                        $(".img_btn").hide();
                        //增加点击选择事件
                        $("body").delegate(".item","click",function(){
                            var toUrl = basePath+"res/html/gwcsubup.html?access_token="+access_token+"&address="+$(this).attr("id");
                            if(GetUrl("id").length>0){
                                toUrl+='&id='+GetUrl("id");
                            }
                            if(GetUrl("ids").length>0){
                                toUrl+='&ids='+GetUrl("ids");
                            }
                            toUrl+="&t="+new Date().getTime();
                            window.location.href= toUrl;
                        });
                    }
                }else{
                    layer.open({content: data.msg,skin: 'msg',time: 2});
                }
            },
            error:function(data) {
            }
        });
    }
    addressList();
</script>
</html>
